package handler

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"zy11/model"
	"zy11/repository"
	"zy11/request"
	"zy11/response"
)

func AddRoom(c *gin.Context) {
	var req request.RoomAdd
	err := c.ShouldBind(&req)
	if err != nil {
		response.ResponseError(c, "错误", err.Error())
		return
	}
	room := model.Room{
		UserId: c.GetUint("userId"),
		Name:   req.Name,
	}
	roomId := repository.Create(&room)
	if roomId == false {
		response.ResponseError(c, "群添加失败", roomId)
	}
	addroomuser := model.RoomUser{
		RoomUserId: room.UserId,
		Auth:       1,
		RoomId:     room.Id,
		Remark:     "群主",
	}
	repository.Create(&addroomuser)
	response.ResponseSuccess(c, "群添加成功", roomId)
}

func AddRoomUser(c *gin.Context) {
	var req []request.RoomUserAdd
	err := c.ShouldBind(&req)
	if err != nil {
		response.ResponseError(c, "错误", err.Error())
		return
	}

	roomUser := model.RoomUser{}
	repository.GetOneByFields(&model.RoomUser{
		RoomUserId: c.GetUint("userId"),
		RoomId:     req[0].RoomId,
	}, &roomUser)
	fmt.Println(roomUser)
	if roomUser.Auth != 1 && roomUser.Auth != 2 {
		response.ResponseError(c, "不是群主或者群管理", nil)
		return
	}
	for _, userAdd := range req {
		var Onse = model.RoomUser{
			RoomUserId: c.GetUint("userId"),
			Auth:       3,
			RoomId:     userAdd.RoomId,
			Remark:     userAdd.Remark,
		}
		repository.Create(&Onse)
	}
	response.ResponseSuccess(c, "群成员添加成功", nil)

}
